#region Header
//=============================================================================
//                          GShield (Game Shield)
//=============================================================================
//
// Original Programmer: Shawn Jackson
// Company: WaveTech Digital Technologies (www.wtdt.com)
// Copyright: (c) 2006 WaveTech Digital Technologies, All Rights Reserved.
// Assembly: Data
// Code File Name: Advisor.cs
// Code File Date: 12/07/2006
//
// Code File Info: Engine stub linking to the GShield Web Service
//=============================================================================
/*
 * UPDATES
 * -------
 *    ShawnJ 5/5/2007 (0.9.0.x): Refactoring for OSS.
 *		0.9.6.x (ShawnJ) 12/24/2007: Moved from EntLib to Linq
 */
#endregion Header

#region License

/*
  GShield - Game Shield
  Copyright (C) 2006-2007 WaveTech Digital Technologies, Inc.
                                http://www.wtdt.com

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

#endregion License

#region Includes

using System;
using WaveTech.GShield.Data;
using WaveTech.GShield.Engine.Managers;
using WaveTech.GShield.Engine.Tools;
using WaveTech.GShield.Model;
using GameRatingDetail=WaveTech.GShield.Engine.GShieldAdvisorService.GameRatingDetail;

#endregion

namespace WaveTech.GShield.Engine
{
	/// <summary>
	/// GShield Advisor Web Service Entry Point
	/// </summary>
	public class Advisor
	{
		private Setting settings;

		#region Constructor

		public Advisor()
		{
			InitializeRequiredSystems();
		}

		#endregion Constructor

		#region Initialize Required Systems

		private void InitializeRequiredSystems()
		{
			settings = SettingsDAO.Get();
		}

		#endregion Initialize Required Systems

		#region Get ESRB Rating

		/// <summary>
		/// Queries a GShield Advisor Web Service to obtain a ESRB game rating using common information.
		/// </summary>
		/// <param name="title">Title of the game</param>
		/// <param name="publisher">Publisher of the game</param>
		/// <param name="fileName">Full name of the game's exe file</param>
		/// <param name="directoryName">Name of the directory the exe resided in</param>
		/// <param name="productName">Product name</param>
		/// <returns>RatingTypes</returns>
		public RatingTypes GetESRBRating(string title, string publisher, string fileName, string directoryName,
		                                string productName)
		{
			int? result;

			GShieldAdvisorService.GShieldAdvisorService service = new GShieldAdvisorService.GShieldAdvisorService();
			service.Url = ServicesDAO.GetRandomServiceUrl();

			try
			{
				// Try the random remote Advisor web service

				if (settings != null && settings.TrimNonExactRatingResults)
					result = service.GetESRBRatingEx(title, publisher, fileName, directoryName, productName,
					                                 settings.TrimNonExactRatingResults);
				else
					result = service.GetESRBRating(title, publisher, fileName, directoryName, productName);
			}
			catch
			{
				// If the remote service failes, try locally.
				ESRBManager _ESRBManager = new ESRBManager();

				if (settings != null && settings.TrimNonExactRatingResults)
					result = _ESRBManager.GetESRBRating(title, publisher, fileName, directoryName, productName,
					                                    settings.TrimNonExactRatingResults);
				else
					result = _ESRBManager.GetESRBRating(title, publisher, fileName, directoryName, productName);
			}

			if (result != null)
				return (RatingTypes) result.Value;
			else
				return RatingTypes.Unknown;
		}

		#endregion Get ESRB Rating

		#region Get ESRB Rating Detail

		/// <summary>
		/// Queries a GShield Advisor Web Service to obtain full ESRB rating information
		/// </summary>
		/// <param name="title">Title of the game</param>
		/// <param name="publisher">Publisher of the game</param>
		/// <param name="fileName">Full name of the game's exe file</param>
		/// <param name="directoryName">Name of the directory the exe resided in</param>
		/// <param name="productName">Product name</param>
		/// <returns>GameRatingDetail information about the game</returns>
		public GameRatingDetail GetESRBGameDetail(string title, string publisher, string fileName, string directoryName,
		                                          string productName)
		{
			GameRatingDetail result;

			GShieldAdvisorService.GShieldAdvisorService service = new GShieldAdvisorService.GShieldAdvisorService();
			service.Url = ServicesDAO.GetRandomServiceUrl();

			try
			{
				// Try the random remote Advisor web service

				if (settings != null && settings.TrimNonExactRatingResults)
					result = service.GetESRBGameDetailEx(title, publisher, fileName, directoryName, productName,
					                                     settings.TrimNonExactRatingResults);
				else
					result = service.GetESRBGameDetail(title, publisher, fileName, directoryName, productName);
			}
			catch
			{
				// If the remote service failes, try locally.
				ESRBManager _ESRBManager = new ESRBManager();

				if (settings != null && settings.TrimNonExactRatingResults)
					result =
						((Object)
						 _ESRBManager.GetESRBGameDetail(title, publisher, fileName, directoryName, productName,
						                                settings.TrimNonExactRatingResults)) as GameRatingDetail;
				else
					result =
						((Object) _ESRBManager.GetESRBGameDetail(title, publisher, fileName, directoryName, productName)) as
						GameRatingDetail;
			}

			if (result != null)
			{
				//TODO: Fix!
        //return RatingConversion.Convert(result);
				return null;
			}
			else
			{
				return null;
			}
		}

		#endregion Get ESRB Rating Detail
	}
}